


import bpy

from 全局控制.Node.节点和插座基类 import bl_UpdateNode



E_数值插座类型枚举 = [
	("C_Socket_Bool",		"Bool", "", 0),
	("C_Socket_F32",		"Float", "", 1),
	("C_Socket_F64", 		"Float64", "", 2),
	("C_Socket_I32",		"Int32", "", 3),
	("C_Socket_I64",		"Int64", "", 4),
	("C_Socket_UI32", 		"UInt32", "", 5),
	("C_Socket_UI64", 		"UInt64", "", 6),

	("C_Socket_Vec2", 		"Vec2", "", 7),
	("C_Socket_Vec3",		"Vec3", "", 8),
	("C_Socket_Vec4",		"Vec4", "", 9),

	("C_Socket_iVec2", 		"Vec2i", "", 10),
	("C_Socket_iVec3", 		"Vec3i", "", 11),
	("C_Socket_uVec3", 		"uVec3", "", 12),
	("C_Socket_iVec4", 		"iVec4", "", 13),
	("C_Socket_uVec4", 		"uVec4", "", 14),
	("C_Socket_顶点索引",	"顶点索引", "", 15),

	("C_Socket_矩阵2X2", 	"Matrix2X2", "", 16),
	("C_Socket_矩阵3X3", 	"Matrix3X3", "", 17),
	("C_Socket_矩阵4X4", 	"Matrix4X4", "", 18),

	("C_Socket_网格",		"Mesh",			"", 19),
	("C_Socket_物体",		"Object",		"", 20),
	("TYPE_插座_LineText",	"LineText",		"", 21),

	("C_Socket_引用",		"引用",			"",	22),
	("C_Socket_Vector模板",	"Vector模板",	"",	23),


	
]




E_创建插座维度枚举 = [
	("TYPE_插座维度_0",	"标量", "", 0),
	("TYPE_插座维度_1",	"1D", "", 1),
	("TYPE_插座维度_2",	"2D", "", 2),
	("TYPE_插座维度_3",	"3D", "", 3),
	("TYPE_插座维度_4", "4D", "", 4),
]








def f_创建插座(node, typeName, 维度, 输出, name=None):
	socket = None
	if 输出 :
		socket = node.outputs
		if name == None : name = 'Output_'
	else:
		socket = node.inputs
		if name == None : name = 'Input_'
		
	if typeName == 'C_Socket_F32':
		if 维度 == 0:
			return socket.new(type='C_Socket_F32', name=name)
		elif 维度 > 0:
			sn = 'C_Socket_F32_Array' + str(维度) + 'D'
			return socket.new(type=sn, name=name)
		

	elif typeName == 'C_Socket_I32':
		if 维度 == 0:
			return socket.new(type='C_Socket_I32', name=name)
		elif 维度 > 0:
			sn = 'C_Socket_I32_Array' + str(维度) + 'D'
			return socket.new(type=sn, name=name)

		
	elif typeName == 'C_Socket_UI32':
		if 维度 == 0:
			return socket.new(type='C_Socket_UI32', name=name)
		elif 维度 > 0:
			sn = 'C_Socket_UI32_Array' + str(维度) + 'D'
			return socket.new(type=sn, name=name)

		
	elif typeName == 'C_Socket_Vec2':
		if 维度 == 0:
			return socket.new(type='C_Socket_Vec2', name=name)
		elif 维度 > 0:
			sn = 'C_Socket_Vec2_Array' + str(维度) + 'D'
			return socket.new(type=sn, name=name)

		
	elif typeName == 'C_Socket_Vec3':
		if 维度 == 0:
			return socket.new(type='C_Socket_Vec3', name=name)
		elif 维度 > 0:
			sn = 'C_Socket_Vec3_Array' + str(维度) + 'D'
			return socket.new(type=sn, name=name)

		
	elif typeName == 'C_Socket_Vec4':
		if 维度 == 0:
			return socket.new(type='C_Socket_Vec4', name=name)
		elif 维度 > 0:
			sn = 'C_Socket_Vec4_Array' + str(维度) + 'D'
			return socket.new(type=sn, name=name)

		
	elif typeName == 'C_Socket_iVec2':
		if 维度 == 0:
			return socket.new(type='C_Socket_iVec2', name=name)
		elif 维度 > 0:
			sn = 'C_Socket_Vec2i_Array' + str(维度) + 'D'
			return socket.new(type=sn, name=name)

		
	elif typeName == 'C_Socket_iVec3':
		if 维度 == 0:
			return socket.new(type='C_Socket_iVec3', name=name)
		elif 维度 > 0:
			sn = 'C_Socket_iVec3_Array' + str(维度) + 'D'
			return socket.new(type=sn, name=name)

		
	elif typeName == 'C_Socket_iVec4':
		if 维度 == 0:
			return socket.new(type='C_Socket_iVec4', name=name)
		elif 维度 > 0:
			sn = 'C_Socket_Vec4i_Array' + str(维度) + 'D'
			return socket.new(type=sn, name=name)

		
	elif typeName == 'C_Socket_矩阵2X2':
		pass
	elif typeName == 'C_Socket_矩阵3X3':
		pass
	elif typeName == 'C_Socket_矩阵4X4':
		if 维度 == 0:
			return socket.new(type='C_Socket_矩阵4X4', name=name)
		elif 维度 > 0:
			sn = 'C_Socket_矩阵4X4_Array' + str(维度) + 'D'
			return socket.new(type=sn, name=name)
	
	
	elif typeName == 'TYPE_插座_顶点索引':
		if 维度 == 0:
			return socket.new(type='C_Socket_顶点索引', name=name)
		elif 维度 > 0:
			sn = 'C_Socket_顶点索引_Array' + str(维度) + 'D'
			return socket.new(type=sn, name=name)


	elif typeName == 'C_Socket_网格':
		if 维度 == 0:
			return socket.new(type='C_Socket_网格', name=name)
		elif 维度 > 0:
			sn = 'C_Socket_网格_Array' + str(维度) + 'D'
			return socket.new(type=sn, name=name)

		
	elif typeName == 'C_Socket_物体':
		if 维度 == 0:
			return socket.new(type='C_Socket_物体', name=name)
		elif 维度 > 0:
			sn = 'C_Socket_物体_Array' + str(维度) + 'D'
			return socket.new(type=sn, name=name)
		
	



	elif typeName == 'C_Socket_引用':
		return socket.new(type='C_Socket_引用', name=name)
		
	elif typeName == 'C_Socket_Vector模板':
		return socket.new(type='C_Socket_Vector模板', name=name)






def f_移除插座(sockets, name):
	for socket in sockets:
		if name == socket.identifier :
			sockets.remove(socket)
			return





def f_取列表单值(valueList):
	name = type(valueList).__name__
	if name == 'list' or name == 'bpy_prop_array':
		#print(type(valueList), valueList)
		return valueList[0]
	return valueList



class C_NodeSocket_基类(bpy.types.NodeSocket, object):
	#bl_socket_idname = '基类'

	def f_init(self, context):
		pass
	def draw(self, context, layout, node, text):
		pass
	def draw_color(self, context, node):
		状态 = self.m_状态
		if 状态 == 0:
			return (0.6, 0.6, 0.3, 1.0)
		elif 状态 == 1:
			return (0.0, 1.0, 0.0, 1.0)
		elif 状态 == -1:
			return (1.0, 0.0, 0.0, 1.0)
		return (0.6, 0.6, 0.3, 1.0)
	def update(self):
		print('更新插座')
	def f_getType(self):
		return self.bl_idname
	def f_setType(self, p_type):
		self.m_Type = p_type
	def f_set状态(self, 状态):
		self.m_状态 = 状态
	def f_getData(self, key=-1):
		if self.is_output == False :
			if self.is_linked :
				插座 = bl_UpdateNode.f_get上一层Output插座(self)
				if 插座 != None:
					return 插座.f_getData(key)
			else:
				return None
		#else:
		#	print(self.name, self.node.name, '插座使用 f_getData 函数错误！ 不能用输出插座取输入插座数据')
		return None
		
	def f_setData(self, data):
		self.f_pushUpdate()
		return False
	def f_setKeyData(self, data, key):
		return False
	def f_insertKeyData(self, data, key=[]):
		return False
		
		
	def f_addData(self, data, 排除重复=False):
		return False
	def f_popData(self, id=[]):
		pass
	def f_close(self):
		pass
	def f_clear(self):
		pass


	def f_set维度(self, dim, T='i', 默认值=-1):
		pass
	def f_get维度(self):
		pass


	def f_pushUpdate(self):
		self.m_是否要更新 = True
		#self.node.m_是否要更新 = True
		#print('插座更新 = ', self.node)
	def f_标记为以更新(self):
		self.m_是否要更新 = False
		#print('插座更新 = f_标记为以更新 ', self.node)
	def f_is未更新(self):
		#print('A 插座更新 = f_is未更新 ', self.node, self.m_是否要更新)
		return self.m_是否要更新
	def f_is以更新(self):
		#print('B 插座更新 = f_is以更新 ', self.node, self.m_是否要更新)
		if self.m_是否要更新 :
			return False
		return True

	def f_set功能状态(self, 状态):
		self.m_功能类型 = 状态
	def f_get功能状态(self):
		return int(self.m_功能类型)
		

def f_连接到插座是否为正确类型(插座, typeName):
	if 插座.is_linked :
		连接到的输出插座 = bl_UpdateNode.f_get上一层Output插座(插座)
		
		if 连接到的输出插座.f_getType() == typeName :
			插座.f_set状态(0)
			return 0
		else:
			插座.f_set状态(-1)
			return -1
	else:
		return 0xffffffff






class C_Socket_基类(bpy.types.NodeSocket):
	#bl_idname = "base_socket"
	default_value = bpy.props.BoolProperty(default=True)
	def __init__(self):
		super(C_Socket_基类, self).__init__()


	@classmethod 
	def bl_rna_get_subclass_py(self, id, default=None):
		print('bl_rna_get_subclass_py = ', self, id, default)
		return None
	@classmethod 
	def bl_rna_get_subclass(self, id, default=None):
		print('bl_rna_get_subclass = ', self, id, default)
		return None
		
	def draw_color(self, context, node):
		print('draw_color = ', self.name, node.name)
		return (0.6, 0.6, 0.3, 1.0)

	def draw(self, context, layout, node, text):
		layout.label(text=text)
		
	
	
	def f_getData(self):
		return None
	def f_setData(self, data):
		return None
	def f_update(self):
		pass
	def f_str(self):
		return ''
	

classes = (
	C_NodeSocket_基类,
	C_Socket_基类,
)


#插座功能类型枚举
#0 普通类型
#1 插座更新不需要确认输入节点更新状态

def register():
	bpy.types.NodeSocket.m_功能类型 = bpy.props.BoolProperty(default=0)
	bpy.types.NodeSocket.m_是否要更新 = bpy.props.BoolProperty(default=True)
	bpy.types.NodeSocket.m_状态 = bpy.props.IntProperty(default=0)

	for c in classes:
		bpy.utils.register_class(c)
	pass	

def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)
	pass
	



		